$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'zc/zcquery',
        datatype: "json",
        colModel: [
            { label: '用户ID', name: 'userId', hidden: true, index: "user_id", width: 45, key: true },
            { label: '用户名', name: 'username', width: 75 },
            { label: '工号', name: 'num', width: 40 },
            { label: '政治面貌', name: 'zmname', width: 75 },
            { label: '职称', name: 'zcname', width: 75 },
            { label: '创建时间', name: 'createTime', index: "create_time", width: 85}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 40,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var zcvm = new Vue({
    el:'#zcapp',
    data:{
        zc:{
            zcusername: null
        },
        zmoptions:[],
        zcoptions:[],
        zcuser:{
            username: null,
            num: null,
            password: null,
            zzmm: null,
            zc: null,
            status: null
        },
        zcshowList: true,
        zctitle:''
    },
    created:function(){
        $.get(baseURL+"zcdict/oplist",function (s) {
            zcvm.zmoptions=s.zmlist;
            zcvm.zcoptions=s.zclist;
        });


    },
    methods: {
        zcquery: function () {
            zcvm.reload();
        },
        zcadd: function(){
            zcvm.zcshowList = false;
            zcvm.zctitle = "新增";
            zcvm.zcuser = {
                username: null,
                num: null,
                password: null,
                zzmm: null,
                status: null
            }

        },
        zcupdate: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
            zcvm.zcshowList = false;
            zcvm.zctitle = "修改";
            //调用后台接口查询一条
            $.get(baseURL + "zc/zcedit/"+userId, function(cc){
                zcvm.zcuser = cc.zcuser;
                zcvm.zcuser.password = null;

            });
        },
        zcsaveOrUpdate: function () {
            $.ajax({
                type: "POST",
                url: baseURL +  "zc/zcadd",
                contentType: "application/json",
                data: JSON.stringify(zcvm.zcuser),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            zcvm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },


        zcdel: function () {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "zc/zcdel",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                zcvm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },

        reload: function () {
            zcvm.zcshowList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'zcusername': zcvm.zc.zcusername},
                page:page
            }).trigger("reloadGrid");
        }

    }
});